#!/bin/bash
##K8S安装部署脚本
install_k8s_install-k8s(){
	service_chek "kubelet"
	service_install "kubelet kubectl kubeadm" 
	chek_status "$?" "K8S安装"
}
install_k8s_images-pull-shell(){
echo '
	images172="registry.aliyuncs.com/google_containers/kube-apiserver
registry.aliyuncs.com/google_containers/kube-controller-manager
registry.aliyuncs.com/google_containers/kube-scheduler
registry.aliyuncs.com/google_containers/kube-proxy"
	for i in $images172;do
	        images="${i}:v1.17.2"
	        docker pull ${images}
	done
	images="registry.aliyuncs.com/google_containers/coredns:1.6.5
registry.aliyuncs.com/google_containers/etcd:3.4.3-0
registry.aliyuncs.com/google_containers/pause:3.1"
	for i in $images;do
        	docker pull $i
	done
' > /init/images_pull.sh
}
install_k8s_install-bash-completion(){
	service_install "bash-completion"
	chek_status "$?" "自动补全工具"
	source /usr/share/bash-completion/bash_completion
	auto="kubectl kubeadm"
	for i in $auto;do
		source <($i completion bash)
		echo "source <($i completion bash)" >> ~/.bashrc
	done
}
install_k8s_init-k8s(){
	mkdir -p /init/
	ver=`kubeadm version|awk '{print $5}'|sed "s/[^0-9|\.]//g"|awk 'NR==1{print}'`
	echo "
	source /init/installer.conf
	echo -e \"K8s version is \" \"v${ver}\"
	echo -e \"Master IP is\" \"\${master_ip}\"
	swapoff -a
	echo \"正在初始化K8S\"
	echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
	kubeadm init --apiserver-advertise-address=${master_ip} \
    --image-repository registry.aliyuncs.com/google_containers \
    --kubernetes-version v${ver} \
    --service-cidr=10.2.11.0/16 \
    --pod-network-cidr=10.244.0.0/16 > /dev/null
	mkdir -p ${HOME}/.kube
	cp -i /etc/kubernetes/admin.conf \${HOME}/.kube/config
	chown \$(id -u):\$(id -g) \${HOME}/.kube/config
	kubectl apply -f https://xypj-1252147235.cos.ap-guangzhou.myqcloud.com/kube-flannel.yaml >/dev/null
	kubeadm token create --print-join-command > z
	zs=\`cat z\`
	echo  \"当前集群节点信息如下\"
	kubectl get nodes
	echo -e \"请在Node节点执行下面的指令以加入此Master节点\"  \"\n\$zs\"
	echo -e \"如需获取注册指令,请在本节点执行下面的命令\n\" \"kubeadm token create --print-join-command\" " > /init/k8s.sh
}
install_k8s_k8s-config(){
	if [[ "$node_type" == "node" ]];then
		red_z_green_z_info "请在Master节点执行下面的命令:" "kubeadm token create --print-join-command"
	elif [[ "$node_type" == "master" ]];then
		if [[ "$ik8s" == "y" || "$ik8s" == "Y" ]];then
			init_k8s
			red_z_green_z_info  "请手动执行下面的指令进行初始化" "bash /init/k8s.sh"
			su -
		else
			green_zt "请自行初始化K8S服务"
		fi

	else
		green_zt "永远相信美好的事情即将发生"
	fi
}
install_k8s_get-k8s-version(){
    kubeamd_ver=`kubeadm version |awk '{print $5}' |sed -e 's/GitVersion:\"//' -e 's/\".*//'`
}
install_k8s_set-node-info(){
	echo '待更新'
}
#判断当前主机信息
config_installer(){
	#判断当前主机是否可以作为Master节点
	cpus=`cat /proc/cpuinfo| grep "processor"| sort| uniq| wc -l`
	if [[ "$cpus" -lt "2" ]];then
		red_z_green_z_info "本机CPU核心数量无法满足Master最低要求[最低2核]\n" "已默认设置本节点为Node节点"

	else
		red_z_green_z "请选择本机节点类型\n" "[1]Master\n" "[2]Node [Default]"
		read types
	fi
	if [[ "$types" == '1' ]];then
		node_type='master'
		input "是否生成初始化K8S脚本[y/n];默认[n]" "ik8s"
		input "请输入Node节点数量" "Node_s"
		if [[ -z "$Node_s" ]];then
			green_zt "未设置Node节点"
		else
			if [[ "$Node_s" -ge "1" ]];then
				hosts_set "$Node_s"
			else
				red_z_green_z "数量输入错误" "本次默认未设置Node节点"
			fi
		fi
		local_hostname_set "master"
		master_ip=$local_ip
	else
		node_type='node'
		input "请输入Matser节点主机名:" "Master_server"
		local_hostname_set "Node"
	fi


		input "是否初始化主机名映射文件[y/n],默认[ y ]" "inits"
	if [[ "$inits" == 'n' ]];then
		green_zt "保留现有hosts文件"
	else
		echo -e "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4\n::1         localhost localhost.localdomain localhost6 localhost6.localdomain6" > /etc/hosts
	fi
	input "先拉取镜像再配置服务:[y/n];默认[n]" "pull"
}